import {isArray} from "./TypeUtil";
import {isDatasetFn} from "../services/PreviewService.tsx";

/**
 * 校验工具类
 */

/**
 * 校验数据
 * @param data 要校验的数据
 * @param validObj 校验对象 {a: 1} 如果对象中a不等于1则返回false
 * @returns {boolean}
 */
// @ts-ignore
const validFun: Function = (data: any, validObj: any): boolean => {
    let isSuc: boolean = true;
    for (let k in validObj) {
        let validTempObj: any = validObj[k];

        let val: any = data[k];
        if (!validTempObj) {
            if (validTempObj !== val) {
                isSuc = false;
            }
        } else if (isArray(validTempObj)) {
            if (validTempObj.indexOf(val) === -1) {
                isSuc = false;
            }
        } else if (typeof (validTempObj) === "object") {
            if (!validFun(val, validTempObj)) {
                isSuc = false;
            }
        } else if (typeof (validTempObj) === "boolean") {
            if (validTempObj !== !!val) {
                isSuc = false;
            }
        } else {
            if (validTempObj !== val) {
                isSuc = false;
            }
        }
    }
    return isSuc;
};

/**
 * 校验数据
 * @param data 要校验的数据 可以是数组 或 对象
 * @param validObj 校验对象 {a: 1} 如果对象中a不等于1则返回false
 * @returns {boolean}
 */
// @ts-ignore
const validField: Function = (data: any, validObj: any): boolean => {
    if (isArray(data)) {
        for (let i in data) {
            if (!validFun(data[i], validObj)) {
                return false;
            }
        }
        return true;
    } else {
        return validFun(data, validObj);
    }
};

const validFieldUn: Function = (data: any, validObj: any): boolean => {
    if (isArray(data)) {
        for (let i in data) {
            if (validFun(data[i], validObj)) {
                return true;
            }
        }
        return false;
    } else {
        return validFun(data, validObj);
    }
};

/**
 * 校验数据
 * @param data 要校验的数据 可以是数组 或 对象
 * @param validObj 校验对象 {a: 1} 如果对象中a不等于1则返回false
 * @returns {boolean}
 */
// @ts-ignore
const onlyValidField: Function = (data: any, validObj: any): boolean => {
    let onlyKey: any, flag: boolean = true;
    if (!isArray(data)) {
        return false;
    }
    if (isArray(validObj)) {
        onlyKey = {}
        for (let i = 0; i < validObj.length; i++) {
            let item: any = validObj[i];
            for (let j = 0; j < data.length; j++) {
                if (onlyKey[item]) {
                    flag = (onlyKey[item] === data[j][item]);
                    if (!flag) {
                        return flag
                    }
                    onlyKey[item] = data[j][item];
                } else {
                    onlyKey[item] = data[j][item]
                }
            }
        }
    } else {
        for (let i = 0; i < data.length; i++) {
            if (onlyKey) {
                flag = (onlyKey === data[i][validObj]);
                if (!flag) {
                    return flag
                }
                onlyKey = data[i][validObj];
            } else {
                onlyKey = data[i][validObj]
            }
        }
    }
    return flag
};

// @ts-ignore
const validDataset: Function = (data: any, typeField: string): boolean => {
    if (isArray(data)) {
        for (let i in data) {
            if (!isDatasetFn(data[i][typeField])) {
                return false;
            }
        }
        return true;
    } else {
        return isDatasetFn(data[typeField]);
    }
}

export {
    validFun,
    validField,
    onlyValidField,
    validDataset,
    validFieldUn
};